package com.daqsoft.controller.system;

import com.daqsoft.data.Module;
import com.daqsoft.dto.GeneralResultEnum;
import com.daqsoft.permission.Verify;
import com.daqsoft.permission.VerifyType;
import com.daqsoft.service.system.ModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 模块管理控制器
 * @author 周麟东 on 10:35
 */
@RestController
@RequestMapping("moduleManager")
@Verify(desc = "模块管理")
public class ModuleManageController {
    @Autowired
    private ModuleService moduleService;

    @RequestMapping("getAllModules")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "获取全部模块")
    public GeneralResultEnum getAllModules(){
        return GeneralResultEnum.SUCCESS.setData(moduleService.getAllByTree()).setDesc("获取成功");
    }
    @RequestMapping("getModulesByPid")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "通过PID获取模块")
    public GeneralResultEnum getModulesByPid(String node){
        List<Module> modules;
        if(node.equals("root")){
            modules = moduleService.getPModules();
        }else {
            int pid = Integer.parseInt(node);
            modules = moduleService.getByPid(pid);
        }
        if (modules == null){
            return GeneralResultEnum.FAIL.setDesc("找不到模块");
        }
        List<Map<String,Object>> map = new ArrayList<>();
        for (Module module : modules) {
            map.add(module.toMap());
        }
        return GeneralResultEnum.SUCCESS.setData(map);
    }

    @RequestMapping("save")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "保存模块")
    public GeneralResultEnum save(Module module , int pid){
        moduleService.saveModule(module , pid);
        return GeneralResultEnum.SUCCESS.setDesc("保存成功");
    }

    @RequestMapping("remove")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "删除模块")
    public GeneralResultEnum remove(int id){
        Module module = moduleService.get(id);
        if (module.getChildren() != null && !module.getChildren().isEmpty()){
            return GeneralResultEnum.FAIL.setDesc("请先删除["+module.getModuleName()+"]下的子模块");
        }
        moduleService.remove(id);
        return GeneralResultEnum.SUCCESS.setDesc("删除成功");
    }

    @RequestMapping("getAllSimpleModules")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "获取全部简单类型的模块")
    public GeneralResultEnum getAllSimpleModules(){
        return GeneralResultEnum.SUCCESS.setData(moduleService.getAll()).setDesc("获取成功");
    }

    @RequestMapping("getSimplePModules")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "获取全部父级模块")
    public GeneralResultEnum getSimplePModules(){
        return GeneralResultEnum.SUCCESS.setData(moduleService.getPModules()).setDesc("获取成功");
    }

    @RequestMapping("getModule")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "获取某个模块")
    public GeneralResultEnum getModule(int id){
        return GeneralResultEnum.SUCCESS.setData(moduleService.get(id)).setDesc("获取成功");
    }

}
